<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
require 'connection.php';
    
 	try{
 		$con = connect();
 		
 		$name = $_POST['name'];
 		$state = $_POST['state'];
 		$dateinitial = $_POST['dateinitial'];
 		$detefinal = $_POST['datefinal'];
 		
 		$sql = "SELECT n.idnotificacion, n.nombre, n.fecha, e.nombre estado FROM notificacion n, estado e
        WHERE n.estado = e.idestado ";
 		
 		if($name != ''){
 			$sql = $sql."AND (";
 			$arreglo = explode(' ', $name);
 			for($i=0;$i<count($arreglo);$i++){
 				if($i > 0){
 					$sql = $sql."AND ";
 				}
 				$sql = $sql."n.nombre LIKE '%".$arreglo[$i]."%' ";
 			}
 			$sql = $sql.") ";
 		}
 		if($state != '' && $state != '0'){
 			$sql = $sql."AND e.idestado='".$state."' ";
 		}
 		if($dateinitial != ''){
 			$sql = $sql."AND n.fecha >='".$dateinitial."' ";
 		}
 		if($detefinal != ''){
 			$sql = $sql."AND n.fecha <='".$detefinal."' ";
 		}
 		$sql = $sql."GROUP BY n.nombre";
 		
 		$result = $con->query($sql);
 		
 		$arr = array();
 		while ($obj = $result->fetch_object()) {
 			$arr[] = array('name' => utf8_encode($obj->nombre),
 					'date' => $obj->fecha,
 					'state' => $obj->estado,
 					'id' => $obj->idnotificacion,
 			);
 		}
 		
 		mysqli_close($con);
 		echo '' . json_encode($arr) . '';
 	}catch (Exception $e){    
         echo $e->getMessage();
    }  
    
?>
